<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
    public function actionInit()
    {
        $auth = Yii::$app->authManager;

        // 添加 "createDoc" 权限
        $createDoc = $auth->createPermission('createDoc');
        $createDoc->description = '新增文章';
        $auth->add($createDoc);

        // 添加 "updatePost" 权限
        $updateDoc = $auth->createPermission('updateDoc');
        $updateDoc->description = '修改文章';
        $auth->add($updateDoc);

        // 添加 "deletePost" 权限
        $deleteDoc = $auth->createPermission('deleteDoc');
        $deleteDoc->description = '删除文章';
        $auth->add($deleteDoc);
        
        // 添加 "approveDoc" 权限
        $approveDoc = $auth->createPermission('approveDoc');
        $approveDoc->description = '审核评论';
        $auth->add($approveDoc);
        

        // 添加 "postadmin" 角色并赋予 "updatePost" “deletePost” “createPost”
        $docAdmin = $auth->createRole('docAdmin');
        $docAdmin->description = '文章管理员';
        $auth->add($docAdmin);
        $auth->addChild($docAdmin, $updateDoc);
        $auth->addChild($docAdmin, $createDoc);
        $auth->addChild($docAdmin, $deleteDoc);
        
        // 添加 "postOperator" 角色并赋予  “deletePost” 
        $docOperator = $auth->createRole('docOperator');
        $docOperator->description = '文章操作员';
        $auth->add($docOperator);
        $auth->addChild($docOperator, $deleteDoc);
        
        // 添加 "DocAuditor" 角色并赋予  “approveDoc”
        $docAuditor = $auth->createRole('docAuditor');
        $docAuditor->description = '评论审核员';
        $auth->add($docAuditor);
        $auth->addChild($docAuditor, $approveDoc);

        // 添加 "admin" 角色并赋予所有其他角色拥有的权限
        $admin = $auth->createRole('admin');
        $admin->description = '系统管理员';
        $auth->add($admin);
        $auth->addChild($admin, $docAdmin);
        $auth->addChild($admin, $docAuditor);
        
        

        // 为用户指派角色。其中 1 和 2 是由 IdentityInterface::getId() 返回的id （译者注：user表的id）
        // 通常在你的 User 模型中实现这个函数。
        $auth->assign($admin, 1);
        $auth->assign($docAdmin, 2);
        $auth->assign($docOperator, 3);
        $auth->assign($docAuditor, 4);
    }
}